package com.freemyleft.left.zapp.net;

/**
 * Created by FreeMyleft on 2017/12/1.
 */

import android.content.Context;

import com.freemyleft.left.zapp.net.callback.IError;
import com.freemyleft.left.zapp.net.callback.IFailure;
import com.freemyleft.left.zapp.net.callback.IRequest;
import com.freemyleft.left.zapp.net.callback.ISuccess;
import com.freemyleft.left.zapp.net.callback.RequestCallbacks;
import com.freemyleft.left.zapp.net.download.DownloadHandler;
import com.freemyleft.left.zapp.ui.LeftLoader;
import com.freemyleft.left.zapp.ui.LoaderStyle;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;

/**
 * 这个RestClient在每次Buider的时候都会生成一个全新的实例，而这里面的参数是一次构建完毕绝不允许更改的
 */
public class RestClient {
    private final String URL;
    private final Map<String, Object> PARAMS  = new HashMap<>();
    private final ISuccess SUCCESS;
    private final IFailure FAILURE;
    private final IRequest REQUEST;
    private final IError ERROR;
    private final RequestBody BODY;
    private final String DOWNLOAD_DIR;
    private final String EXTENSION;//后缀名
    private final String NAME;
    private final File FILE;
    private LoaderStyle LOADER_STYEL;
    private final Context CONTEXT;

    public RestClient(String url, Map<String, Object> params,String download_dir,String extenston,String name,
                      ISuccess success, IFailure failure, IRequest request,
                      IError error, RequestBody body,Context context,LoaderStyle loaderStyle,File file) {
        this.DOWNLOAD_DIR=download_dir;
        this.EXTENSION=extenston;
        this.NAME=name;
        this.URL = url;
        PARAMS.putAll(params);
        this.SUCCESS = success;
        this.FAILURE = failure;
        this.REQUEST = request;
        this.ERROR = error;
        this.BODY = body;
        this.FILE =file;
        this.CONTEXT =context;
        this.LOADER_STYEL=loaderStyle;

    }

    // 创建创建者
    public static RestClentBuilder builder() {
        return new RestClentBuilder();

    }

    private void request(HttpMethod method) {

        final RestService service = RestCreator.getRestService();
        Call<String> call = null;

        if (REQUEST != null) {
            REQUEST.onRequestStart();
        }
        if (LOADER_STYEL!=null){
            LeftLoader.showLoading(CONTEXT,LOADER_STYEL);
        }
        switch (method) {
            case GET:
                call = service.get(URL, PARAMS);
                break;
            case POST:
                call = service.post(URL, PARAMS);
                break;
            case POST_RAW:
                call=service.postRaw(URL,BODY);
                break;
            case PUT:
                call = service.put(URL, PARAMS);
                break;
            case PUT_RAW:
                call=service.putRaw(URL,BODY);

            case DELETE:
                call = service.delete(URL, PARAMS);
                break;
            case UPLOAD:
                final  RequestBody requestBody=RequestBody.create(MediaType.parse(MultipartBody.FORM.toString()),FILE);
                final  MultipartBody.Part body=MultipartBody.Part.createFormData("file",FILE.getName(),requestBody);

                call=RestCreator.getRestService().upload(URL,body);
                break;
            default:
                break;
        }
        //Retrofit给我们提供了两个请求的方法 exqueue这是在主线程请求 enqueue在后台另起一个线程并不会影响到我们的UI线程

        if (call != null) {
            call.enqueue(getRequestCallback());

        }
    }

    private Callback<String> getRequestCallback() {
        return new RequestCallbacks(
                REQUEST,SUCCESS,FAILURE,ERROR,LOADER_STYEL);
    }

    public  final void get(){
        request(HttpMethod.GET);
    }
    public  final void post(){
        if (BODY==null){
            request(HttpMethod.POST);
        }else {
            if (!PARAMS.isEmpty()){
                throw  new  RuntimeException("参数不能为空");
            }
            request(HttpMethod.POST_RAW);
        }
       // request(HttpMethod.POST);
    }
    public  final void put(){
        if (BODY==null){
            request(HttpMethod.PUT);
        }else {
            if (!PARAMS.isEmpty()){
                throw  new  RuntimeException("参数不能为空");
            }
            request(HttpMethod.PUT_RAW);
        }
    }
    public  final void delete(){
        request(HttpMethod.DELETE);
    }
    public  final void upload(){
        request(HttpMethod.UPLOAD);
    }
    public final void download(){
       new DownloadHandler(URL,SUCCESS,FAILURE, REQUEST,ERROR,DOWNLOAD_DIR,EXTENSION,NAME).handleDownload();
    }

}
